package com.example.Controller;


import com.example.util.Result;
import com.example.util.sms.SMSUtil;
import org.assertj.core.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/sms")
public class SmsController {


    @Autowired
    private SMSUtil smsUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/sendSms")
    public Result sendSms(@RequestBody Map<String,String> map){
        String phone = map.get("phone");
        String sms =  smsUtil.generateSMS(phone);
        if(!Strings.isNullOrEmpty(sms)){
            //
            return new Result("200",sms);
        }

        return new Result("500", "验证码错误");
    }


    /**
     * 校验短信验证码
     * @param phone 手机号
     * @param validCode 短信验证码
     * @return 校验结果
     */
    @GetMapping("/checkSms")
    public Result checkSms(@RequestParam("phone") String phone, @RequestParam("validCode") String validCode){
        //check 检查，核对；
        boolean result = smsUtil.validateSMS(phone, validCode);

        if(result){
            return new Result("200", "验证码正确");
        }

        return new Result("500", "验证码错误");
    }

}
